package com.ruyixing.shopmanager.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;

import com.ruyixing.shopmanager.http.RYHttpClient;
import com.ruyixing.shopmanager.http.RYHttpResponseHandler;
import com.ruyixing.shopmanager.model.Login;

public class LoginHelper {

	public final static int ERROR_EMPTY_USERNAME = 1;
	public final static int ERROR_EMPTY_PASSWORD = 2;
	public final static int ERROR_EMAIL_NOT_EXISTS = 3;
	public final static int ERROR_WRONG_PASSWORD = 4;

	private final static String PREFERENCES_NAME = "login_info";
	private final static String PREFERENCES_IS_LOGIN = "is_login";
	private final static String PREFERENCES_BDUSS = "bduss";
	private final static String PREFERENCES_NICKNAME = "nick_name";
	// private final static String PREFERENCES_EMAIL = "email";
	private final static String PREFERENCES_USERNAME = "user_name";
	private final static String PREFERENCES_CITY = "user_city";
	private final static String PREFERENCES_SIGNIN = "user_signin";
	private final static String PREFERENCES_REVIEW = "user_review";
	private final static String PREFERENCES_PHOTO = "user_photo";
	private final static String PREFERENCES_AVATAR_URL = "user_avatar_url";

	public static LoginHelper s_instance;

	private Context mContext;
	private ILoginListener mLoginListener;

	private LoginHelper(Context context) {
		mContext = context;
	}

	public static boolean init(Context context) {
		if (context == null) {
			return false;
		}
		s_instance = new LoginHelper(context);
		return true;
	}

	public static LoginHelper getInstance() {
		return s_instance;
	}

	public boolean isLogin() {
		SharedPreferences preferences = getSharedPreferences();
		if (preferences == null) {
			return false;
		}
		boolean isLogin = preferences.getBoolean(PREFERENCES_IS_LOGIN, false);
		return isLogin;
	}

	public boolean doLogin(String username, String password,
			ILoginListener listener) {
		if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)
				|| listener == null) {
			return false;
		}
		mLoginListener = listener;

		SharedPreferences preferences = getSharedPreferences();
		preferences.edit().putString(PREFERENCES_USERNAME, username).commit();

		RYHttpClient.doLogin(mContext, mRyHttpResponseHandler, username,
				password);
		return true;
	}

	public void doLogout() {
		SharedPreferences preferences = getSharedPreferences();
		Editor editor = preferences.edit();
		editor.remove(PREFERENCES_BDUSS);
		editor.putBoolean(PREFERENCES_IS_LOGIN, false);
		editor.remove(PREFERENCES_NICKNAME);
		editor.remove(PREFERENCES_AVATAR_URL);
		editor.remove(PREFERENCES_CITY);
		editor.remove(PREFERENCES_PHOTO);
		editor.remove(PREFERENCES_REVIEW);
		editor.remove(PREFERENCES_SIGNIN);
		editor.commit();
	}

	public String getUserName() {
		return getSharedPreferences().getString(PREFERENCES_USERNAME, "");
	}

	public String getAvatarUrl() {
		return getSharedPreferences().getString(PREFERENCES_AVATAR_URL, "");
	}

	public String getUserCity() {
		return getSharedPreferences().getString(PREFERENCES_CITY, "");
	}

	public int getUserPhotoCount() {
		return getSharedPreferences().getInt(PREFERENCES_PHOTO, 0);
	}

	public int getUserReviewCount() {
		return getSharedPreferences().getInt(PREFERENCES_REVIEW, 0);
	}

	public int getUserSigninCount() {
		return getSharedPreferences().getInt(PREFERENCES_SIGNIN, 0);
	}

	public String getNickName() {
		if (isLogin()) {
			return getSharedPreferences().getString(PREFERENCES_NICKNAME, "");
		}
		return "";
	}

	public String getBDUSS() {
		if (isLogin()) {
			String bduss = getSharedPreferences().getString(PREFERENCES_BDUSS,
					"");
			System.out.println("LoginHelper.getBDUSS() bduss=" + bduss);
			return bduss;
		}
		return "";
	}

	private SharedPreferences getSharedPreferences() {
		return mContext.getSharedPreferences(PREFERENCES_NAME,
				Context.MODE_MULTI_PROCESS);
	}

	private void loginSuccess(Login login) {
		SharedPreferences preferences = getSharedPreferences();
		Editor editor = preferences.edit();
		editor.putString(PREFERENCES_BDUSS, login.bduss);
		editor.putString(PREFERENCES_AVATAR_URL, login.avatar);
		editor.putString(PREFERENCES_CITY, login.livecity);
		editor.putInt(PREFERENCES_PHOTO, login.photo);
		editor.putInt(PREFERENCES_REVIEW, login.review);
		editor.putInt(PREFERENCES_SIGNIN, login.signin);
		editor.putBoolean(PREFERENCES_IS_LOGIN, true);
		editor.putString(PREFERENCES_NICKNAME, login.nickname);
		System.out.println("LoginHelper.loginSuccess() bduss=" + login.bduss);
		editor.commit();
	}

	private RYHttpResponseHandler mRyHttpResponseHandler = new RYHttpResponseHandler() {

		@Override
		public void onSuccess(Object o) {
			loginSuccess((Login) o);
			mLoginListener.onLoginSuccess();
		}

		@Override
		public void onFailure(Throwable throwable) {
			mLoginListener.onLoginFail(ERROR_WRONG_PASSWORD);
		}
	};

	public static interface ILoginListener {
		public void onLoginSuccess();

		public void onLoginFail(int errorNo);
	}
}
